package com.zaoniao.enstudy.api.controller;

import com.zaoniao.enstudy.api.dto.VocabularyWordsAddDto;
import com.zaoniao.enstudy.api.dto.VocabularyWordsDelDto;
import com.zaoniao.enstudy.api.dto.VocabularyWordsUpdDto;
import com.zaoniao.enstudy.domain.entity.bo.VocabularyWordsBo;
import com.zaoniao.enstudy.domain.service.VocabularyWordsService;
import com.zaoniao.enstudy.infracore.constants.AppConstant;
import com.zaoniao.enstudy.infracore.web.BaseResponse;
import com.zaoniao.enstudy.infracore.web.DataResponse;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Tag(name = "词汇表单词明细相关Api")
@RestController
@RequestMapping("/vocabulary-words")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VocabularyWordsController {

    private final VocabularyWordsService vocabularyWordsService;

    /**
     * 收藏单词.
     *
     * @param addWordDto 收藏单词前端入参.
     * @return -
     */
    @Operation(summary = "收藏单词", description = "参考抖音收藏")
    @PostMapping
    public BaseResponse addVocabularyWords(@Valid @RequestBody VocabularyWordsAddDto addWordDto) {
        vocabularyWordsService.addVocabularyWords(VocabularyWordsAddDto.Converter.INSTANCE.dto2do(addWordDto));
        return DataResponse.ok("收藏成功");
    }

    /**
     * 取消收藏单词.
     *
     * @param delWordDto 取消收藏单词前端入参.
     * @return -
     */
    @Operation(summary = "取消收藏单词", description = "删除词汇表以及该表下的所有单词，避免误删，采用逻辑删除")
    @DeleteMapping
    public BaseResponse delUserVocabularyWords(@Valid @RequestBody VocabularyWordsDelDto delWordDto) {
        vocabularyWordsService.updStatus(delWordDto.getVocabularyWordsId());
        return DataResponse.ok("取消收藏成功");
    }

    /**
     * 修改收藏单词的词汇表.
     *
     * @param updWordDto 修改收藏单词的词汇表前端入参.
     * @return -
     */
    @Operation(summary = "更换词汇表", description = "已收藏单词更换到另一个词汇表")
    @PutMapping
    public BaseResponse updUserVocabularyWords(@Valid @RequestBody VocabularyWordsUpdDto updWordDto) {
        vocabularyWordsService.updVocabularyWords(VocabularyWordsUpdDto.Converter.INSTANCE.dto2do(updWordDto));
        return DataResponse.ok("更换词汇表成功");
    }

    /**
     * 分页查询收藏单词列表.
     *
     * @param vocabularyId 词汇表Id
     * @param page         第几页
     * @param size         每一页显示的条数
     * @return - 单词列表
     */
    @Operation(summary = "词汇表名下单词列表查询", description = "下拉刷新，采用分页查询词汇表--根据词汇表查询")
    @GetMapping
    public DataResponse<PagedGridResult<VocabularyWordsBo>> vocabularyWordsPage(
            @Parameter(name = "vocabularyId", description = "词汇表Id") @RequestParam Integer vocabularyId,
            @Parameter(name = "page", description = "第几页", allowEmptyValue = true)
            @RequestParam(value = "page", required = false) Integer page,
            @Parameter(name = "size", description = "每一页显示的条数", allowEmptyValue = true)
            @RequestParam(value = "size", required = false) Integer size) {
        if (page == null) {
            page = AppConstant.PAGE;
        }
        if (size == null) {
            size = AppConstant.SIZE;
        }
        PagedGridResult<VocabularyWordsBo> pagedGridResult = vocabularyWordsService.getVocabularyWordsPage(vocabularyId, page, size);
        return DataResponse.ok(pagedGridResult);
    }

}
